/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.calculator;

import com.jy.appmarket.AppMarketApplication;
import com.jy.appmarket.R;
import com.jy.appmarket.screenlock.LockScreenManager;
import com.jy.appmarket.screenlock.statistics.ClassificationEvent;
import com.jy.appmarket.screenlock.statistics.StatisticsEventName;
import com.rad.statistics.android.StatisticsAgent;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class CalculatorActivity extends Activity {
	public final static int FROM_HOMEPAGE = 1;
	public final static int FROM_DESKTOP = 2;
	public final static int FROM_LOCK_SCREEN = 3;
	public final static int FROM_NOTIFICATION = 4;
	public final static String EXTRA_IN_ACTIVITY = "in_activity";
	EventListener mListener = new EventListener();
	private CalculatorDisplay mDisplay;
	private Persist mPersist;
	private History mHistory;
	private Logic mLogic;
	private AutoResizeTextView mInputTv;
	private int mWhereFrom;

	// private static final int HVGA_WIDTH_PIXELS = 320;

	static final int BASIC_PANEL = 0;
	static final int ADVANCED_PANEL = 1;

	@Override
	public void onCreate(Bundle state) {
		super.onCreate(state);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.calculator_main);

		mPersist = new Persist(this);
		mHistory = mPersist.history;

		mDisplay = findViewById(R.id.display);
		mInputTv = findViewById(R.id.input_text);

		mLogic = new Logic(this, mHistory, mDisplay, mInputTv);
		HistoryAdapter historyAdapter = new HistoryAdapter(this, mHistory, mLogic);
		mHistory.setObserver(historyAdapter);
		mListener.setHandler(mLogic);
		// mDisplay.setOnKeyListener(mListener);
		whereFrom();
		// 统计打开计算器
		MobclickAgent.onEvent(this, "page_view", "PV_计算器");
		StatisticsAgent.onEvent(this, new ClassificationEvent("page_view", StatisticsEventName.PV_CALCULATOR));
	}

	private void whereFrom() {
		if (getIntent() != null) {
			mWhereFrom = getIntent().getIntExtra(EXTRA_IN_ACTIVITY, FROM_DESKTOP);
			if (mWhereFrom == FROM_DESKTOP) {
				MobclickAgent.onEvent(this, "shortcut_click", "SC_计算器");
				StatisticsAgent.onEvent(this,
						new ClassificationEvent("shortcut_click", StatisticsEventName.SC_CALCULATOR));
			} else if (mWhereFrom == FROM_NOTIFICATION) {
				MobclickAgent.onEvent(this, "tools_bar_click", "TBC_计算器");
				StatisticsAgent.onEvent(this,
						new ClassificationEvent("tools_bar_click", StatisticsEventName.TBC_CALCULATOR));
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		mLogic.updateHistory();
		mPersist.save();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mWhereFrom == FROM_LOCK_SCREEN && AppMarketApplication.sIsPauseLocked) {
			LockScreenManager.getInstance(this).lock();
		}
	}
	// /**
	// * The font sizes in the layout files are specified for a HVGA display.
	// * Adjust the font sizes accordingly if we are running on a different
	// * display.
	// */
	// public void adjustFontSize(TextView view) {
	// float fontPixelSize = view.getTextSize();
	// Display display = getWindowManager().getDefaultDisplay();
	// int h = Math.min(display.getWidth(), display.getHeight());
	// float ratio = (float)h/HVGA_WIDTH_PIXELS;
	// view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize*ratio);
	// }
}
